package com.huajx.tuopuda.client.udp.codec.auth;

import com.huajx.tuopuda.client.udp.TuopudaDirect;
import com.huajx.tuopuda.client.udp.TuopudaUtil;
import com.huajx.tuopuda.client.udp.codec.AbstractCodec;

import java.util.Date;

/**
 *
 * 尾部加权限[109B]
 *
 * @author: lisa 叶新华
 * @create: 2019-09-28 03:56
 */
public class AppendAuthCodec extends AbstractCodec {
    private int function = TuopudaDirect.D_109B;
    private int cardId;
    private int index;
    private int startYmh=0x2721; //2019-09-01
    private int endYmh=0x299f; // 2020-12-31

    private int password=0x01E240; // 123456

    protected AppendAuthCodec(int sn) {
        setSn(sn);
    }

    protected AppendAuthCodec(int sn, int cardId, int index) {
        this(sn);
        this.cardId = cardId;
        this.index = index;
    }

    protected AppendAuthCodec(int sn, int cardId, int index, Date start, Date end, int password) {
        this(sn, cardId, index);

        this.startYmh = start == null ? this.startYmh : TuopudaUtil.dateToDateInt(start);
        this.endYmh = end == null ? this.endYmh : TuopudaUtil.dateToDateInt(end);
        this.password = password;
    }

    public static AppendAuthCodec getInstance(int sn, int cardId, int index) {
        return new AppendAuthCodec(sn, cardId, index);
    }

    public static AppendAuthCodec getInstance(int sn, int cardId, int index, Date start, Date end, int password) {
        return new AppendAuthCodec(sn, cardId, index, start, end, password);
    }

    @Override
    public byte[] encode() {
        return encode(getSn(), getFunction(), (bytes) -> {
            int checkSum = 0;
            checkSum += TuopudaUtil.replaceBytes(bytes, getIndex(), 5, 2);
            checkSum += TuopudaUtil.replaceBytes(bytes, getCardId(), 7, 4);
            checkSum += TuopudaUtil.replaceBytes(bytes, getStartYmh(), 11, 2);
            checkSum += TuopudaUtil.replaceBytes(bytes, getEndYmh(), 13, 2);
//            checkSum += TuopudaUtil.replaceBytes(bytes, 0x00, 15, 1); // 时段，默认设置为0
            checkSum += TuopudaUtil.replaceBytes(bytes, getPassword(), 16, 3);
            checkSum += TuopudaUtil.replaceBytes(bytes, 0x01010101, 19, 4);
            return checkSum;
        });
    }

    public int getCardId() {
        return cardId;
    }

    public void setCardId(int cardId) {
        this.cardId = cardId;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getStartYmh() {
        return startYmh;
    }

    public void setStartYmh(int startYmh) {
        this.startYmh = startYmh;
    }

    public int getEndYmh() {
        return endYmh;
    }

    public void setEndYmh(int endYmh) {
        this.endYmh = endYmh;
    }

    public int getPassword() {
        return password;
    }

    public void setPassword(int password) {
        this.password = password;
    }

    public int getFunction() {
        return function;
    }

    protected void setFunction(int function) {
        this.function = function;
    }
}
